Imports System.Xml.Serialization
Public Class ClassNames
    Private Const XmlFilename As String = "PlayersSpellAndClass.xml"

    Public Enum ClassIndex As Integer
        Unknown
        Any
        Warrior
        Paladin
        Hunter
        Rogue
        Priest
        Shaman
        Mage
        Warlock
        Druid
        DeathKnight
    End Enum

    Public ClassNames() As String = {"Unknown", "Any", "Warrior", "Paladin", "Hunter", "Rogue", "Priest", "Shaman", "Mage", "Warlock", "Druid", "Death Knight"}
    Public ClassNamesShort() As String = {"unk", "any", "war", "pal", "hun", "rog", "pri", "sha", "mag", "lck", "drd", "dkn"}

    Public Structure SpellEntry
        <XmlAttribute()> Dim spell As String
        <XmlAttribute()> Dim spellId As Integer

        <XmlAttribute()> Dim prefix As String
        <XmlAttribute()> Dim suffix As String

        <XmlAttribute()> Dim classIdx As ClassIndex
        <XmlAttribute()> Dim build As String
    End Structure

    Public PlayersSpells() As SpellEntry


    Public Sub New()
        Dim XU As New XmlUtils.AccessXml

        ReDim PlayersSpells(0)
        PlayersSpells(0) = New SpellEntry
        PlayersSpells(0).spell = "Unknown"

        Try
            XU.ImportFromXml(PlayersSpells, getXmlFilename(XmlFilename))
        Catch ex As Exception
            Dim EH As New ErrorHandler
            EH.ErrorMsgBox(ex, "ClassName.New ImportFromXml")
        End Try
    End Sub

    Protected Overrides Sub Finalize()
        Dim XU As New XmlUtils.AccessXml
        Try
            XU.ExportByXml(PlayersSpells, getXmlFilename(XmlFilename))
        Catch ex As Exception
            Dim EH As New ErrorHandler
            EH.ErrorMsgBox(ex, "ClassName.Finalize ExportByXml")
        End Try

        MyBase.Finalize()
    End Sub

    Private Function getXmlFilename(ByVal filename As String) As String
        Dim path As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
        Return My.Computer.FileSystem.CombinePath(path, filename)
    End Function

    Public Function GuessClassName(ByVal SpellName As String, ByVal SpellID As Integer, ByVal ClassIdx As ClassIndex, ByVal UpdateSpellDb As Boolean) As ClassIndex
        Dim i As Integer
        Dim EH As New ErrorHandler

        For i = 0 To PlayersSpells.Length - 1
            If PlayersSpells(i).spellId = SpellID Then
                If UpdateSpellDb Then
                    If PlayersSpells(i).classIdx = ClassIndex.Unknown Then
                        PlayersSpells(i).classIdx = ClassIdx
                    ElseIf PlayersSpells(i).classIdx = ClassIndex.Any Then
                        '
                    Else
                        If PlayersSpells(i).classIdx = ClassIdx Then
                            '
                        Else
                            PlayersSpells(i).classIdx = ClassIndex.Any
                        End If
                    End If
                    Return ClassIdx
                Else
                    If PlayersSpells(i).classIdx = ClassIndex.Unknown Then
                        Return ClassIdx
                    ElseIf PlayersSpells(i).classIdx = ClassIndex.Any Then
                        Return ClassIdx
                    Else
                        Return PlayersSpells(i).classIdx
                    End If
                End If
            End If
        Next

        If PlayersSpells.Length = 1 And PlayersSpells(0).spell = "Unknown" Then
            '
        Else
            ReDim Preserve PlayersSpells(PlayersSpells.Length)
        End If
        PlayersSpells(PlayersSpells.Length - 1) = New SpellEntry
        PlayersSpells(PlayersSpells.Length - 1).classIdx = ClassIndex.Unknown
        PlayersSpells(PlayersSpells.Length - 1).spellId = SpellID
        PlayersSpells(PlayersSpells.Length - 1).spell = SpellName
        'PlayersSpells(PlayersSpells.Length - 1).eventName = EventName

        EH.ErrorLog("GuessClassName", "GuessClassName", "Added Unknown Class", "Spell=" & SpellName)

        Return ClassIndex.Unknown
    End Function

    Public Function GetClassIndex(ByVal ClassName As String) As ClassIndex
        Dim i As Integer

        For i = 0 To ClassNames.Length - 1
            If ClassNames(i) = ClassName Then
                Return i
            End If
        Next

        Return ClassIndex.Unknown
    End Function

    Public Function GetClassIndexByShort(ByVal ShortClassName As String) As ClassIndex
        Dim i As Integer

        For i = 0 To ClassNames.Length - 1
            If ClassNamesShort(i) = ShortClassName Then
                Return i
            End If
        Next

        Return ClassIndex.Unknown
    End Function
End Class

